//
//  GeneralSettingViewController.m
//  QRMaster
//
//  Created by lc on 16/4/28.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "GeneralSettingViewController.h"
#import "SettingCell.h"
#import "GuestViewController.h"
#import "ModifyPWDViewController.h"
#import "localizControllerViewController.h"
#import "aboutViewController.h"
#import "OpertionPlist.h"
#import "GesturePasswordController.h"
#import "UsingGuideController.h"

@interface GeneralSettingViewController ()<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate>

@property (nonatomic,strong) UITableView * setttingTableView;

@property (nonatomic,strong) NSArray * sectionOneArray;
@property (nonatomic,strong) NSArray * sectionTwoArray;
@property (nonatomic,strong) NSArray * sectionThreeArray;
@property (nonatomic,strong) NSArray * sectionFourArray;

@property (nonatomic,strong) NSUserDefaults * userInfo;

@property (nonatomic,strong) UISwitch * soundSwitch;
//@property (nonatomic,strong) UISwitch * guideSwitch;
@property (nonatomic,strong) UISwitch * quickSwitch;

//@property (nonatomic,assign) BOOL isOpenFlag ; //是否打开用户指引

@end

@implementation GeneralSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //数组初始化
    [self initData];
    
    [self prepareTableView];
    
    //注册settingCell
    [_setttingTableView registerNib:[UINib nibWithNibName:@"SettingCell" bundle:0]  forCellReuseIdentifier:@"settingCell"];
    
    _userInfo = [NSUserDefaults standardUserDefaults];
//    _isOpenFlag = NO;
    
    
}

-(void)viewWillAppear:(BOOL)animated{
   
    self.navigationItem.title = NSLocalizedString(@"mineSetting", nil);
    [self initData];
    
//    _isOpenFlag = NO;
    
    [_setttingTableView reloadData];
}

- (void)initData{
   
    _sectionOneArray = @[NSLocalizedString(@"modifyGesturePWD", nil),NSLocalizedString(@"soundSwitch", nil),NSLocalizedString(@"mineGuide", nil),NSLocalizedString(@"quickEnter", nil)];
    _sectionTwoArray = @[NSLocalizedString(@"modifyPWD", nil)];
    _sectionThreeArray = @[NSLocalizedString(@"lauguage", nil)];
    _sectionFourArray = @[NSLocalizedString(@"mineAbout", nil),NSLocalizedString(@"quit", nil)];

}


- (void)prepareTableView{
    _setttingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _setttingTableView.backgroundColor = [UIColor clearColor];
    _setttingTableView.dataSource = self;
    _setttingTableView.delegate = self;
    _setttingTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_setttingTableView];
    
    _soundSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH-67, 12, 51, 31)];
//    _guideSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH-67, 12, 51, 31)];
    _quickSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH-67, 12, 51, 31)];
    
}


#pragma mark - tableView dataSource和delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0){
        return _sectionOneArray.count;
    }else if (section==1){
        return _sectionTwoArray.count;
    }else if (section==2){
        return _sectionThreeArray.count;
    }else{
        return _sectionFourArray.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * ID = @"settingCell";
    SettingCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SettingCell" owner:self options:nil] firstObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section==0){
        cell.titleLabel.text = _sectionOneArray[indexPath.row];
        if (indexPath.row==1){
            cell.nextImage.hidden = YES;
            
            if ([[_userInfo objectForKey:@"sound"] isEqualToString:@"1"]) {
                _soundSwitch.on = YES;
            }
            else
            {
                _soundSwitch.on = NO;
            }
            
            [_soundSwitch addTarget:self action:@selector(soundSwitch:) forControlEvents:UIControlEventValueChanged];
            [cell.contentView addSubview:_soundSwitch];
        }
        if (indexPath.row==2) {
            cell.nextImage.hidden = NO;
            
//            _guideSwitch.on = _isOpenFlag;
//            [_guideSwitch addTarget:self action:@selector(openUserGuide:) forControlEvents:UIControlEventValueChanged];
//            [cell.contentView addSubview:_guideSwitch];
        }
        if (indexPath.row == 3) {
            cell.nextImage.hidden = YES;
            if ([[_userInfo objectForKey:@"quickCard"] isEqualToString:@"1"]) {
                _quickSwitch.on = YES;
            }
            else
            {
                _quickSwitch.on = NO;
            }
            
            [_quickSwitch addTarget:self action:@selector(openQuickSwitch:) forControlEvents:UIControlEventValueChanged];
            [cell.contentView addSubview:_quickSwitch];
        }
    }else if (indexPath.section==1){
        cell.titleLabel.text = _sectionTwoArray[indexPath.row];
    }else if (indexPath.section==2){
        cell.titleLabel.text = _sectionThreeArray[indexPath.row];
    }else{
        cell.titleLabel.text = _sectionFourArray[indexPath.row];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0 && indexPath.row==0){
        
        [self guestClick];
        
    }else if (indexPath.section==0 && indexPath.row==2){
        
        [self useGuide];
    
    }else if (indexPath.section==1){
        
        [self LoginPWDClick];
        
    }else if (indexPath.section==2){
        
        if (indexPath.row==0) {
            
            [self localizClick];
            
        }else{
        
        }
    }else if (indexPath.section==3){
        
        if (indexPath.row==0) {
            
            [self AboutClick];
            
        }else{
            
            [self QuitClick];
            
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 16;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 56;
}

#pragma mark - switch开关
/**
 *  函数描述：点击修改声音的开关切换
 *  输入参数：UISwitch的对象
 *  输出参数：无
 *  返回值：  无
 */
-(void)soundSwitch:(UISwitch *)ch
{
    if (ch.on) {
        [_userInfo setValue:@"1" forKey:@"sound"];
    }
    else
    {
        [_userInfo setValue:@"0" forKey:@"sound"];
    }
    [_userInfo synchronize];
    [_setttingTableView reloadData];
}

//#pragma mark - 用户指引
//-(void)openUserGuide:(UISwitch *)openSwitch
//{
//    [_userInfo setValue:@"yes" forKey:@"middle"];             //快捷按钮提示操作
//    [_userInfo setValue:@"yes" forKey:@"share"];              //分享按钮提示操作
//    [_userInfo setValue:@"yes" forKey:@"setlock"];            //授权分配卡切换提示操作
//    [_userInfo setValue:@"yes" forKey:@"faverit"];            //收藏房卡提示操作
//    [_userInfo setValue:@"yes" forKey:@"address"];            //从通讯录添加成员
//    [_userInfo setValue:@"yes" forKey:@"changeCommunity"];    //切换集群提示操作
//    [_userInfo setValue:@"yes" forKey:@"longPress"];          //长按分享
//    [_userInfo synchronize];
////    _isOpenFlag = openSwitch.on;
//    [_setttingTableView reloadData];
//}

#pragma mark ---------- 快捷房卡（直达）
-(void)openQuickSwitch:(UISwitch *)quick
{
    if (quick.on) {
        [_userInfo setValue:@"1" forKey:@"quickCard"];
    }
    else
    {
        [_userInfo setValue:@"0" forKey:@"quickCard"];
    }
    [_userInfo synchronize];
    [_setttingTableView reloadData];
}


#pragma mark - 点击事件

#pragma mark - 使用指南
-(void)useGuide{
    UsingGuideController * useGuideVC = [[UsingGuideController alloc] init];
    [self.navigationController pushViewController:useGuideVC animated:YES];
}

#pragma mark - 手势密码
-(void)guestClick
{
    GuestViewController * gvc = [[GuestViewController alloc]init];
    [self.navigationController pushViewController:gvc animated:YES];
}


#pragma mark - 修改密码点击
-(void)LoginPWDClick
{
    ModifyPWDViewController * mfvc = [[ModifyPWDViewController alloc]init];
    [self.navigationController pushViewController:mfvc animated:YES];
}



#pragma mark - 语言选择点击
-(void)localizClick
{
    localizControllerViewController * lcv = [[localizControllerViewController alloc]init];
    [self.navigationController pushViewController:lcv animated:YES];
}


#pragma mark - 关于锁掌柜点击
-(void)AboutClick
{
    aboutViewController * avc = [[aboutViewController alloc]init];
    [self.navigationController pushViewController:avc animated:YES];
}


#pragma mark - 退出点击
-(void)QuitClick
{
    UIActionSheet * quitAcitonSheet = [[UIActionSheet alloc]initWithTitle:NSLocalizedString(@"quitSheet", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"alterCancel", nil) destructiveButtonTitle:NSLocalizedString(@"quit", nil) otherButtonTitles:nil];
    [quitAcitonSheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self logOut];
    }
}

/**
 *  函数描述：退出登录，清除token,手势，以及本地文件
 *  输入参数：无
 *  输出参数：无
 *  返回值：  无
 */
-(void)logOut
{
    [_userInfo setValue:nil forKey:@"token"];
    [_userInfo setValue:nil forKey:@"login"];
    NSMutableDictionary * data = [OpertionPlist readDataFromPlist];
    [data setValue:nil forKey:HOUSEINFO];
    [data setValue:nil forKey:STAFFINFO];
    [data setValue:nil forKey:COMMUNITYINFOFORKEY];
    QRSingleton * single = [QRSingleton sharedQRSingleton];
    single = nil;
    [OpertionPlist writeDataToPlist:data];
    GesturePasswordController * gpc = [[GesturePasswordController alloc]init];
    [gpc clear];
    [_userInfo setValue:@"0" forKey:@"gesture"];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:@"logout" forKey:@"startUp"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"startUp" object:postDict];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
